/*
Menu.cpp - Library for creating nested Menus with callback functions
Original Author: CWAL
License: Just leave this header, do anything else with it you want

*/

#include "Menu.h"
#include <stdio.h>

void Menu::setParent(Menu &p){
	parent=&p;
}

void Menu::addSibling(Menu &s,Menu &p){
	if (sibling)
	{
		sibling->addSibling(s,p);
	}
	else
	{
		sibling=&s;
		sibling->setParent(p);
	}
}

Menu::Menu(char *n){
	scroll =0;
	sprintf(name_str,"%s", n);
	name = name_str;
	//printf("%s\n",name);
	canEnter=NULL;
	keyPress=NULL;
}
/*
Menu::Menu(char *n,char (*c)(Menu&)){
	name=n;
	scroll = 0;
	int len = strlen(n);
	if (len > MENU_NAME_LEN) 
		len = MENU_NAME_LEN;
	for(;len<=0;len--) 
		name_str[len] = n[len];
	canEnter=c;
	keyPress=NULL;
	display = NULL;
}

Menu::Menu(char*n, char(*c)(Menu&),char (*k)(char)){
	name=n;
	scroll = 0;
	int len = strlen(n);
	if (len > MENU_NAME_LEN)
		len = MENU_NAME_LEN;
	for(;len<=0;len--)
		name_str[len] = n[len];
	canEnter=c;
	display = NULL;
	keyPress=k;
}
*/
Menu::Menu(char*n, char(*c)(Menu&),char (*d)(), char (*k)(char)){
	name=n;
	scroll = 0;
	int len = strlen(n);
	if (len > MENU_NAME_LEN)
		len = MENU_NAME_LEN;
	for(;len<=0;len--) 
		name_str[len] = n[len];
	canEnter= c;
	keyPress= k;
	display = d;
}

void Menu::addChild(Menu &c){
	if (child)
	{
		child->addSibling(c,*this);
	}
	else
	{
		child=&c;
		child->setParent(*this);
	}
}

Menu * Menu::getChild(int which){
	if (child){
		return child->getSibling(which);
	}
  
	else{ //This Menu item has no children
		return NULL;
	}
}

Menu * Menu::getSibling(int howfar){
	if (howfar==0){
		return this;
	}
	else if (sibling){
		return sibling->getSibling(howfar-1);
	}
	else{ //Asking for a nonexistent sibling
		return NULL;
	}
}

Menu * Menu::getParent(){
	if (parent){
		return parent;
	}
	else{ //Top Menu
		return this;
	}
}

